{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import uuid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 两个id初次可以从插件读取，在每次获取authcode时作为请求头的一部分，响应的cookies会更新这两个值\n",
    "remid = ''\n",
    "sid = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用remid和sid获取access_token\n",
    "res_access_token = requests.get(\n",
    "    url=\"https://accounts.ea.com/connect/auth\",\n",
    "    params= {\n",
    "        'client_id': 'ORIGIN_JS_SDK',\n",
    "        'response_type': 'token',\n",
    "        'redirect_uri': 'nucleus:rest',\n",
    "        'prompt': 'none',\n",
    "        'release_type': 'prod'\n",
    "    },\n",
    "    headers= {\n",
    "        'Cookie': f'remid={remid};sid={sid}',\n",
    "        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36',\n",
    "        'content-type': 'application/json'\n",
    "    }\n",
    ")\n",
    "access_token = res_access_token.json()['access_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 更新sid和remid\n",
    "def upd_remid_sid(res: requests.Response, remid, sid):\n",
    "    res_cookies = requests.utils.dict_from_cookiejar(res.cookies)\n",
    "    if 'sid' in res_cookies:\n",
    "        sid = res_cookies['sid']\n",
    "    if 'remid' in res_cookies:\n",
    "        remid = res_cookies['remid']\n",
    "    return remid, sid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "remid, sid = upd_remid_sid(res_access_token, remid, sid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取authcode\n",
    "res_authcode = requests.get(\n",
    "    url=\"https://accounts.ea.com/connect/auth\",\n",
    "    params= {\n",
    "        'client_id': 'sparta-backend-as-user-pc',\n",
    "        'response_type': 'code',\n",
    "        'release_type': 'none'\n",
    "    },\n",
    "    headers= {\n",
    "        'Cookie': f'remid={remid};sid={sid}'\n",
    "    },\n",
    "    allow_redirects=False\n",
    ")\n",
    "# 这个请求默认会重定向，所以要禁用重定向，并且重定向地址里的code参数就是我们想要的authcode\n",
    "authcode = str.split(res_authcode.next.path_url, \"=\")[1]\n",
    "\n",
    "remid, sid = upd_remid_sid(res_authcode, remid, sid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用authcode获取sessionID\n",
    "res_session = requests.post(\n",
    "    url=\"https://sparta-gw.battlelog.com/jsonrpc/pc/api\",\n",
    "    json= {\n",
    "        'jsonrpc': '2.0',\n",
    "        'method': 'Authentication.getEnvIdViaAuthCode',\n",
    "        'params': {\n",
    "            'authCode': authcode,\n",
    "            \"locale\": \"zh-tw\",\n",
    "        },\n",
    "        \"id\": str(uuid.uuid4())\n",
    "    }\n",
    ")\n",
    "sessionID = res_session.json()['result']['sessionId']\n",
    "sessionID"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用sessionID示例， 获取欢迎信息\n",
    "res = requests.post(\n",
    "    url=\"https://sparta-gw.battlelog.com/jsonrpc/pc/api\",\n",
    "    json = {\n",
    "        'jsonrpc': '2.0',\n",
    "        'method': 'Onboarding.welcomeMessage',\n",
    "        'params': {\n",
    "            'game' : \"tunguska\",\n",
    "            'minutesToUTC' : \"-480\"\n",
    "        },\n",
    "        \"id\": str(uuid.uuid4())\n",
    "    },\n",
    "    headers= {\n",
    "        'Cookie': f'remid={remid};sid={sid}',\n",
    "        'X-GatewaySession': sessionID\n",
    "    },\n",
    ")\n",
    "res.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用gameid获取服务器信息\n",
    "res = requests.post(\n",
    "    url=\"https://sparta-gw.battlelog.com/jsonrpc/pc/api\",\n",
    "    json = {\n",
    "        'jsonrpc': '2.0',\n",
    "        'method': 'GameServer.getServerDetails',\n",
    "        'params': {\n",
    "            'game' : 'tunguska',\n",
    "            'gameId': '8460532130979'\n",
    "        },\n",
    "        \"id\": str(uuid.uuid4())\n",
    "    },\n",
    "    headers= {\n",
    "        'X-GatewaySession': sessionID\n",
    "    },\n",
    ")\n",
    "res.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
